#!/usr/bin/python

import pyExcelerator
import re, string
import cgi
import cgitb

def init( ret, fileName ) :
	ls = ret.split()
	ssn = ls[0]	
	
	#print "Creating Schedule #" + ssn
	docSheet1 = w.add_sheet("schedule #" + ssn)
	
	docSheet1.write(0,1,'Monday')
	docSheet1.write(0,2,'Tuesday')
	docSheet1.write(0,3,'Wednesday')
	docSheet1.write(0,4,'Thursday')
	docSheet1.write(0,5,'Friday')
	docSheet1.write(0,6,'Saturday')
	docSheet1.write(0,7,'Sunday')

	#assuming only 30 minute intervals between events and a complete 24 hour cycle. Later this will be fine 	tuned so user can choose exactly what he wants.(or detect the smallest interval to determine the times 		displayed on the 	spreadsheet)

	interval=30
	intervalIndex= ((60/interval) * 24) + 2
	hour=12
	minute=0
	half=" AM"
	indexList=[]
	for i in range(1,intervalIndex):
		if minute<10 :
			docSheet1.write(i,0,`hour`+":0"+`minute`+half)
			indexList.append(`i`+" "+`hour`+":0"+`minute`+half)		
		else :
			docSheet1.write(i,0,`hour`+":"+`minute`+half)		
			indexList.append(`i`+" "+`hour` + ":"+`minute`+half)		
		minute+=interval
		if minute >=60:
			minute-=60
			hour+=1
			if hour == 13 : hour=1
		if hour == 12 and minute==0 : 
			half= " AM" if half==" PM" else " PM"
	
	#This is where the input gets loaded into the current spreadsheet
	#use ls
	i= 1
	while i < len(ls) :
		current = ls[i]
		#ls is the list of eventnew() predicates from output of the solver
		if "eventnew" in current :			
			eventList = re.split("[(,)]",current)
			#now we want to find where the event fits onto the spreadsheet
			#index position 4 and 5 have the start and finish time, respectively
			#you need to make sure that the start hour is two digits, other wise it wont match later i.e 1200 vs 800			
			start=eventList[4]
			finish=eventList[5]
			
			startHour=""
			startMinute=""
			finishHour=""
			finishMinute=""
			if len(str(start)) > 3 :
				startHour=start[0:2]
				startMinute=start[2:5]
			else :				
				startHour=start[0:1]
				startMinute=start[1:4]
			
			if len(str(finish)) > 3 :
				finishHour= finish[0:2]
				finishMinute=finish[2:5]
			else :
				finishHour=finish[0:1]
				finishMinute=finish[1:4]
			
			result1=""
			result2=""
			if int(startHour) > 12 :
				result1 = str(int(startHour)-12) +":"+startMinute
			else :
				result1= startHour+":"+startMinute
			if int(startHour) >=12 :
				result1+=" PM"
			else :
				result1+=" AM"

			if int(finishHour) > 12 :
				result2 = str(int(finishHour)-12) +":"+finishMinute
			else :
				result2= finishHour+":"+finishMinute
			if int(finishHour) >=12 :
				result2+=" PM"
			else :
				result2+=" AM"
				
			#now we can search in our index list for the right place to put this sheet
			fswitch=False

			col = eventList[3]
			if col == "m" : 
				col = 1
			elif col == "t" :
				col = 2
			elif col == "w" :
				col = 3
			elif col == "th" :
				col = 4
			elif col == "f" :
				col = 5
			elif col == "sa" :
				col = 6
			elif col == "su" :
				col = 7
		
			for num in range(len(indexList)) : 
				
				row = (indexList[num]).split()[0]
				findit=re.findall("\d+\s"+result1,indexList[num])
				#use regex to find 
				if len(findit) >0 :
					fswitch=True
				if fswitch :
					ender=result2 in indexList[num]
					if ender :
						break		
					docSheet1.write(int(row),int(col),""+eventList[1][0:5] + "," + eventList[2])
					docSheet1.col(int(col)).width = len(""+eventList[1][0:5] + "," + eventList[2])*256
					
					
						
		i+=1
	#procedure: save the file with a random fucking name, and then create a link on the site to it. User can then rename it and dl to anywhere they want.	
	
     
	w.save('/exports/'+fileName)
	


def retrieve(exportList, fileName) :
	file_input = open("output.txt")
	out = re.split("answer: ",file_input.read().lower())
	file_input.close()

	for ind in exportList :
		ret = out[int(ind)]
		init(ret,fileName)
		

def main():		
	
	
	print
	cgitb.enable()
	#print "lets try to retrieve the parameters and print them out </br>"
	f = str(cgi.FieldStorage())
	#We need to store the parameters given into lists
	#print f	
	#print "</br>"
	fileName = f[f.find("'")+1:f.find("&")]
	#print fileName
	list=f[f.find("=")+1:f.find(")")-1]
	#print f
	exportList = re.split(",",list)
	#print exportList
	
	#print "Creating xls Document"
	#print "Retrieving data"			
	retrieve(exportList,fileName)

w = pyExcelerator.Workbook()	
main()

